package org.tuoren.official.controller;

import java.util.ArrayList;
import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import org.tuoren.common.excel.core.DropDownOptions;
import org.tuoren.common.idempotent.annotation.RepeatSubmit;
import org.tuoren.common.log.annotation.Log;
import org.tuoren.common.web.core.BaseController;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.core.domain.R;
import org.tuoren.common.core.validate.AddGroup;
import org.tuoren.common.core.validate.EditGroup;
import org.tuoren.common.log.enums.BusinessType;
import org.tuoren.common.excel.utils.ExcelUtil;
import org.tuoren.official.domain.bo.ProductLineBo;
import org.tuoren.official.domain.vo.ProductImportVo;
import org.tuoren.official.domain.vo.ProductLineVo;
import org.tuoren.official.domain.vo.ProductVo;
import org.tuoren.official.domain.bo.ProductBo;
import org.tuoren.official.es.esmapper.EsProductMapper;
import org.tuoren.official.service.IProductLineService;
import org.tuoren.official.service.IProductService;
import org.tuoren.common.mybatis.core.page.TableDataInfo;

/**
 * 产品信息
 *
 * @author wl
 * @date 2024-11-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/official/product")
public class ProductController extends BaseController {

    private final IProductService productService;
    private final IProductLineService lineService;

    /**
     * 前台-查询产品信息列表
     */
    @SaIgnore
    @GetMapping("/frontList")
    public TableDataInfo<ProductVo> frontList(ProductBo bo, PageQuery pageQuery) {
        return productService.queryPageList(bo, pageQuery);
    }

    /**
     * 前台-获取产品信息详细信息
     *
     * @param id 主键
     */
    @SaIgnore
    @GetMapping("/front/{id}")
    public R<ProductVo> frontGetInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(productService.queryById(id));
    }
    /**
     * 查询产品信息列表
     */
    @SaCheckPermission("official:product:list")
    @GetMapping("/list")
    public TableDataInfo<ProductVo> list(ProductBo bo, PageQuery pageQuery) {
        return productService.queryPageList(bo, pageQuery);
    }

    /**
     * 产品线下拉框
     */
    @SaCheckPermission("official:productLine:list")
    @GetMapping("/lineList")
    public TableDataInfo<ProductLineVo> lineList(ProductLineBo bo) {
        return TableDataInfo.build(lineService.queryList(bo));
    }

    /**
     * 导出产品信息列表
     */
    @SaCheckPermission("official:product:export")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductBo bo, HttpServletResponse response) {
        List<ProductVo> list = productService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品信息", ProductVo.class, response);
    }

    /**
     * 获取产品信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("official:product:query")
    @GetMapping("/{id}")
    public R<ProductVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(productService.queryById(id));
    }

    /**
     * 新增产品信息
     */
    @SaCheckPermission("official:product:add")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductBo bo) {
        return toAjax(productService.insertByBo(bo));
    }

    /**
     * 修改产品信息
     */
    @SaCheckPermission("official:product:edit")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductBo bo) {
        return toAjax(productService.updateByBo(bo));
    }

    /**
     * 删除产品信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("official:product:remove")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(productService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    @SaCheckPermission("official:product:import")
    public void importTemplate(HttpServletResponse response) {
        List<DropDownOptions> options = new ArrayList<>();
        //获取所有的产品线名称
        List<ProductLineVo> productLineVos = lineService.queryList(new ProductLineBo());
        //获取所有的产品线名称
        List<String> productLineNames = productLineVos.stream().map(ProductLineVo::getName).toList();
        options.add(new DropDownOptions(1, productLineNames));
        ExcelUtil.exportExcel(new ArrayList<>(), "官网产品数据", ProductImportVo.class, response, options);
    }

    /**
     * 导入数据
     * @param file          导入文件
     */
    @Log(title = "产品信息", businessType = BusinessType.IMPORT)
    @SaCheckPermission("official:product:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        List<ProductImportVo> result = ExcelUtil.importExcel(file.getInputStream(), ProductImportVo.class);
        productService.importData(result);
        return R.ok();
    }
}
